<template>
	<view class="business">
		<view class="head" :style="{height: pHeight + tHeight + 'px'}">
			<view class="xztitle" :style="[{height: pHeight + 'px', top: tHeight + 'px',lineHeight: pHeight + 'px'}]">
				提现
				<view class="iconfont icon-dayuhao1" @click="fanhui"></view>
			</view>
		</view>
		<view class="headtop clearfix" :style="{marginTop: pHeight + tHeight + 'px'}"></view>
		<view class="mendian">
			<view class="heads">
				<view class="left clearfix">
					<image :src="baseUrl + '/wximage/hktubiao.png'"></image>
					<view class="name">提现方式</view>
				</view>
			</view>
			<!-- #ifdef MP-WEIXIN -->
			<view class="shuju clearfix">
				<view class="name fl">微信钱包</view>
			</view>
			<!-- #endif -->
			
			<!-- #ifdef MP-ALIPAY -->
			<view class="shuju clearfix">
				<view class="name fl">支付宝钱包</view>
				<view class="right fr clearfix" @click="topage('/subpackageA/user/fund/zhanghu')">
					<view class="desc fl">绑定提现账户</view>
					<image :src="baseUrl + '/wximage/right.png'" class="fl"></image>
				</view>
			</view>
			<!-- #endif -->
		</view>
		<view class="mendian mendians">
			<view class="heads">
				<view class="left clearfix">
					<image :src="baseUrl + '/wximage/hktubiao.png'"></image>
					<view class="name">提现金额<text>(最低提现金额{{minmoney}}元，最高{{maxmoney}}元)</text></view>
				</view>
			</view>
			<view class="shuju clearfix">
				<view class="name fl">可提现金额：￥{{ktxmoney}}元</view>
			</view>
			<view class="shuju clearfix">
				<view class="left fl">
					<input type="text" placeholder="请输入提现金额" v-model="txmoney"  @input="inputAmount"/>
				</view>
				<view class="you fr" @click="qbtixian">全部提现</view>
			</view>
			<view class="shuju clearfix">
				<view class="shouxufei fl">手续费：{{shouxufei}}%</view>
				<view class="sjdaozhang fr">实际到账：{{dzmoney}}元</view>
			</view>
		</view>
		<view class="footer">
			<view class="submit" @click="submit">确定</view>
		</view>
		<view class="beijing" v-if="hidden" @click="hidden = false"></view>
		<view class="tankuang" v-if="hidden">
			<view class="title">验证信息</view>
			<view class="form">
				<view class="list clearfix">
					<text class="name">手机号</text>
					<input type="number" maxlength="11" placeholder="请输入手机号" v-model="tell" disabled />
				</view>
				<view class="list clearfix">
					<input type="number" maxlength="11" placeholder="请输入短信验证码" v-model="code" />
					<view class="fasong" v-if="time == 60 || time == 0" @click="fasong">获取验证码</view>
					<view class="fasong" v-else>{{time}}S重新获取</view>
				</view>
			</view>
			<view class="queren" @click="queren">确认</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: getApp().globalData.baseUrl,
				headerTop: 0,
				pHeight: 0,
				tHeight: 0,
				minmoney: '',
				maxmoney: '',
				ktxmoney: 0.00,
				txmoney: '',
				shouxufei: '',
				dzmoney: '0.00',
				hidden: false,
				tell: '',
				name: '',
				id_card: '',
				code: '',
				time: 60,
				timer: null,
			}
		},
		onLoad() {
			// 头部距离
			let res = uni.getSystemInfoSync() ///微信api方法
			let titleH;
			if (res && res['system']) {
				// 判断是否是安卓操作系统 （标题栏苹果为44px,安卓为48px）
				if (res['system'].indexOf('Android') > 0) {
					titleH = 48
				} else {
					titleH = 46
				}
				this.headerTop = titleH + res['statusBarHeight']
				this.pHeight = titleH
				this.tHeight = res['statusBarHeight']
			}
			
			//获取基本配置信息
			this.request({
				url: '/xcx/webconfig',
				data: {}
			}).then(res => {
				if (res.data.code == 200) {
					this.minmoney = res.data.data.minmoney;
					this.maxmoney = res.data.data.maxmoney;
					this.shouxufei = res.data.data.shouxufei;
				} else {
					uni.showToast({
						title: '请求失败，请稍后再试',
						icon: 'none'
					})
				}
			})
			
			let agent_id = uni.getStorageSync('agent_id');
			
			this.request({
				url: '/agent/users',
				data: {
					id: agent_id,
				},
				method: "POST"
			}).then(res => {
				if (res.data.code == 200) {
					this.tell = res.data.data.mobile;
					this.name = res.data.data.zsname;
					this.id_card = res.data.data.id_card;
				} else {
					uni.showToast({
						title: '请求失败，请稍后再试',
						icon: 'none'
					})
				}
			})
			
			this.request({
				url: '/xcx/usersfund',
				data: {
					agent_id: agent_id,
					role_id: uni.getStorageSync('role_id'),
				},
				method: "POST"
			}).then(res => {
				if (res.data.code == 200) {
					this.ktxmoney = res.data.data.ktxmoney
				} else {
					uni.showToast({
						title: '请求失败，请稍后再试',
						icon: 'none'
					})
				}
			})
		},
		methods: {
			// 提现
			submit() {
				let txmoney = this.txmoney;
				let ktxmoney = this.ktxmoney;
			
				if(this.txmoney == ''){
					uni.showToast({
						title: '请输入提现金额',
						icon: 'none'
					})
					return false;
				}
				if (parseFloat(txmoney) > parseFloat(ktxmoney)) {
					uni.showToast({
						title: '提现金额不能大于余额',
						icon: 'none'
					})
					this.txmoney = ''
					this.dzmoney = '0.00'
					return false;
				}
				if (parseFloat(txmoney) < parseFloat(this.minmoney) || parseFloat(txmoney) > parseFloat(this.maxmoney)) {
					uni.showToast({
						title: '提现金额不在合理范围内',
						icon: 'none'
					})
					this.txmoney = ''
					this.dzmoney = '0.00'
					return false;
				}
				
				// #ifdef MP-ALIPAY
				if(this.name == null || this.name == ''){
					uni.showToast({
						title: '请绑定提现账号',
						icon: 'none'
					})
					return false;
				}
				if(this.id_card ==  null || this.id_card == ''){
					uni.showToast({
						title: '请绑定提现账号',
						icon: 'none'
					})
					return false;
				}
				// #endif
				
				this.dzmoney = (parseFloat(this.txmoney) - parseFloat(this.txmoney * this.shouxufei/100)).toFixed(2);
				this.hidden = true;
			},
			fasong(){
				let that =  this;
				
				this.request({
					url: '/xcx/send_code',
					data: {
						tell: this.tell,
					},
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						uni.showToast({
							title: '发送成功',
							icon: 'none'
						})
						
						// 定时60秒
						that.timer = setInterval(() => {
							that.time = that.time - 1;
							if(that.time==0){
								clearTimeout(that.timer);
								that.timer = null;
								that.time = 60;
							}
						},1000);
					} else {
						uni.showToast({
							title: '发送失败',
							icon: 'none'
						})
						return false;
					}
				})
			},
			queren(){
				if(this.code == ''){
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
					
					return false;
				}
				
				// #ifdef MP-WEIXIN
				this.request({
					url: '/xcxmoney/cashagent',
					data: {
						users_id: uni.getStorageSync('user_id'),
						agent_id: uni.getStorageSync('agent_id'),
						money: this.dzmoney,
						yuanmoney: this.txmoney,
						role_id: uni.getStorageSync('role_id'),
						tell: this.tell,
						code: this.code,
					},
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						uni.showToast({
							title: '成功,请耐心等待',
							icon: 'none'
						})
						
						setTimeout(function(){
							uni.navigateBack()
						},2000)
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
						return false;
					}
				})
				// #endif
				
				// #ifdef MP-ALIPAY
				this.request({
					url: '/alipay/cashagent',
					data: {
						users_id: uni.getStorageSync('user_id'),
						agent_id: uni.getStorageSync('agent_id'),
						money: this.dzmoney,
						yuanmoney: this.txmoney,
						role_id: uni.getStorageSync('role_id'),
						tell: this.tell,
						code: this.code,
						name: this.name,
						id_card: this.id_card,
					},
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						uni.showToast({
							title: '成功,请耐心等待',
							icon: 'none'
						})
						
						setTimeout(function(){
							uni.navigateBack()
						},2000)
					} else {
						uni.showToast({
							title: '失败，请稍后再试',
							icon: 'none'
						})
						return false;
					}
				})
				// #endif
			},
			// 全部提现
			qbtixian() {
				this.txmoney = this.ktxmoney;
				this.dzmoney = (parseFloat(this.txmoney) - parseFloat(this.txmoney * this.shouxufei/100)).toFixed(2);
			},
			inputAmount(e) {
				this.dzmoney = (parseFloat(this.txmoney) - parseFloat(this.txmoney * this.shouxufei/100)).toFixed(2);
				if (e.detail.value == '.') {
					this.$nextTick(() => {
						this.txmoney = '';
					})
					console.log(1)
					return;
				} else if ((e.detail.value.split('.').length - 1) > 1) { 
					this.$nextTick(() => {
						this.txmoney = this.txmoney.substring(0, this.txmoney.length - 1);
					})
					console.log(2)
					return;
				} else if (e.detail.value.split('.').length > 1) { //小数点只能输入一个
					if ((e.detail.value.split('.')[1].length > 2)) {
						this.$nextTick(() => {
							this.txmoney = this.txmoney.substring(0, this.txmoney.length - 1);
						})
						console.log(3)
						return;
					}
				}
			},
			topage(url){
				uni.navigateTo({
					url
				})
			},
			fanhui() {
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.business {
		width: 100%;
		margin: 0 auto;
		height: 100vh;
		background: #f9f9ff;
		overflow-y: scroll;

		.head {
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);

			.xztitle {
				position: fixed;
				left: 0%;
				top: 0;
				width: 100%;
				text-align: left;
				color: #333333;
				font-size: 32rpx;
				padding-left: 74rpx;
				font-weight: 700;

				.iconfont {
					position: absolute;
					top: 0;
					left: 30rpx;
					font-size: 44rpx;
				}
			}
		}

		.headtop {
			width: 100%;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);
			height: 300rpx;
			padding: 20rpx 40rpx 0;
			box-sizing: border-box;
		}
		
		.mendian{
			width: 90%;
			margin: 0 auto;
			z-index: 70;
			margin-top: -280rpx;
			background: #FFFFFF;
			padding: 24rpx 20rpx 0;
			border-radius: 20rpx;
			box-shadow: rgba(223, 230, 255, 0.67) 0px 0px 6px;
			
			.heads{
				width: 100%;
				box-sizing: border-box;
				border-bottom: 1rpx solid #F1F1F1;
				padding-bottom: 20rpx;
				
				.left{
					
					image{
						float: left;
						width: 40rpx;
						height: 40rpx;
						margin-top: -6rpx;
					}
					
					.name{
						font-size: 28rpx;
						color: #333333;
						float: left;
						
						text{
							font-size: 24rpx;
							color: #999999;
						}
					}
				}
				
				.right{
					
					image{
						float: left;
						width: 40rpx;
						height: 40rpx;
						margin-top: -6rpx;
					}
					
					.name{
						font-size: 28rpx;
						color: #368BF8;
						float: left;
					}
				}
			}
			
			.shuju{
				width: 100%;
				padding: 30rpx 26rpx;
				box-sizing: border-box;
				border-bottom: 1rpx solid #F1F1F1;
				
				.name{
					font-size: 32rpx;
					color: #333333;
				}
				
				.right{
					margin-top: 6rpx;
					
					.desc{
						font-size: 24rpx;
						color: #979797;
					}
					
					image{
						width: 30rpx;
						height: 30rpx;
					}
				}
				
				.left{
					width: 60%;
				}
				
				.you{
					text-align: right;
					font-size: 28rpx;
					color: #368BF8;
				}
				
				.shouxufei{
					font-size: 24rpx;
					color: #999999;
				}
				
				.sjdaozhang{
					font-size: 24rpx;
					color: #333333;
				}
			}
			
			.shuju:last-child{
				border: none;
			}
		}
		
		.mendians{
			margin-top: 20rpx;
		}
		
		.footer {
			width: 100%;
			margin: 0 auto 40rpx;
			margin-top: 100rpx;
			height: 72rpx;
			text-align: center;
		
			.submit {
				width: 240rpx;
				height: 72rpx;
				line-height: 72rpx;
				background: linear-gradient(90deg, #65bdfc, #398ef8);
				margin-left: 248rpx;
				border-radius: 36rpx;
				border-top-left-radius: 0;
				border-bottom-left-radius: 42rpx;
				color: #FFFFFF;
				font-size: 32rpx;
			}
		}
		
		.beijing{
			width: 100%;
			position: fixed;
			height: 100vh;
			top: 0;
			left: 0;
			z-index: 120;
			background: #000000;
			opacity: 0.5;
		}
		
		.tankuang{
			position: fixed;
			left: 10%;
			top: 34%;
			width: 80%;
			background: #ffffff;
			z-index: 130;
			border-radius: 20rpx;
			
			.title{
				width: 100%;
				text-align: center;
				margin: 40rpx auto;
				font-size: 32rpx;
			}
			
			.form{
				width: 90%;
				margin: 0 auto;
				
				.list{
					width: 100%;
					height: 80rpx;
					line-height: 80rpx;
					margin-bottom: 30rpx;
					
					.name{
						float: left;
						width: 18%;
					}
					
					// #ifdef MP-WEIXIN
					input{
						float: left;
						width: 60%;
						height: 70rpx;
						line-height: 70rpx;
						border: 1rpx solid #f5f5f5;
						padding-left: 5%;
					}
					//#endif
					
					// #ifdef MP-ALIPAY
					input{
						float: left;
						width: 58%;
						height: 60rpx;
						line-height: 60rpx;
						border: 1rpx solid #f5f5f5;
						padding-left: 5%;
					}
					//#endif
					
					.fasong{
						float: left;
						width: 28%;
						margin-left: 30rpx;
						background: #1082e2;
						color: #ffffff;
						text-align: center;
						height: 70rpx;
						line-height: 70rpx;
						border-radius: 10rpx;
						font-size: 24rpx;
					}
				}
			}
			
			.queren{
				width: 40%;
				margin: 100rpx auto 60rpx;
				background: linear-gradient(90deg, #65bdfc, #398ef8);
				color: #ffffff;
				text-align: center;
				height: 70rpx;
				line-height: 70rpx;
				border-radius: 10rpx;
				font-size: 24rpx;
			}
		}
	}
</style>